package com.uls.web.controller.czj;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.czj.domain.CzjBuilding;
import com.uls.czj.service.ICzjBuildingService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.czj.domain.CzjPublichousing;
import com.uls.czj.service.ICzjPublichousingService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 公房管理Controller
 * 
 * @author czj
 * @date 2024-11-10
 */
@RestController
@RequestMapping("/czj/publichousing")
public class CzjPublichousingController extends BaseController
{
    @Autowired
    private ICzjPublichousingService czjPublichousingService;

    @Autowired
    private ICzjBuildingService czjBuildingService;
    /**
     * 查询公房管理列表
     */
    @PreAuthorize("@ss.hasPermi('czj:publichousing:list')")
    @GetMapping("/list")
    public TableDataInfo list(CzjPublichousing czjPublichousing)
    {

        startPage();
        List<CzjPublichousing> list = czjPublichousingService.selectCzjPublichousingList(czjPublichousing);
        for (CzjPublichousing czjPublichousing1:list){
            CzjBuilding czjBuilding= czjBuildingService.selectCzjBuildingByBuildingid(czjPublichousing1.getBuildingid());
            czjPublichousing1.setBuilding(czjBuilding.getLocation()+czjBuilding.getBuildingname());
        }
        return getDataTable(list);
    }

    /**
     * 导出公房管理列表
     */
    @PreAuthorize("@ss.hasPermi('czj:publichousing:export')")
    @Log(title = "公房管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CzjPublichousing czjPublichousing)
    {
        List<CzjPublichousing> list = czjPublichousingService.selectCzjPublichousingList(czjPublichousing);
        ExcelUtil<CzjPublichousing> util = new ExcelUtil<CzjPublichousing>(CzjPublichousing.class);
        util.exportExcel(response, list, "公房管理数据");
    }

    /**
     * 获取公房管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('czj:publichousing:query')")
    @GetMapping(value = "/{roomid}")
    public AjaxResult getInfo(@PathVariable("roomid") Long roomid)
    {
        return success(czjPublichousingService.selectCzjPublichousingByRoomid(roomid));
    }

    /**
     * 新增公房管理
     */
    @PreAuthorize("@ss.hasPermi('czj:publichousing:add')")
    @Log(title = "公房管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CzjPublichousing czjPublichousing)
    {
        CzjBuilding czjBuilding= czjBuildingService.selectCzjBuildingByBuildingid(czjPublichousing.getBuildingid());
        czjBuilding.setBuildingtype("1");
        czjBuildingService.updateCzjBuilding(czjBuilding);

        czjBuildingService.selectCzjBuildingList(czjBuilding);
        return toAjax(czjPublichousingService.insertCzjPublichousing(czjPublichousing));
    }

    /**
     * 修改公房管理
     */
    @PreAuthorize("@ss.hasPermi('czj:publichousing:edit')")
    @Log(title = "公房管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CzjPublichousing czjPublichousing)
    {
        return toAjax(czjPublichousingService.updateCzjPublichousing(czjPublichousing));
    }

    /**
     * 删除公房管理
     */
    @PreAuthorize("@ss.hasPermi('czj:publichousing:remove')")
    @Log(title = "公房管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{roomids}")
    public AjaxResult remove(@PathVariable Long[] roomids)
    {
        for (Long l:roomids){
            Long id= czjPublichousingService.selectCzjPublichousingByRoomid(l).getBuildingid();
            CzjBuilding czjBuilding= czjBuildingService.selectCzjBuildingByBuildingid(id);
            czjBuilding.setBuildingtype("0");
            czjBuildingService.updateCzjBuilding(czjBuilding);
        }
        return toAjax(czjPublichousingService.deleteCzjPublichousingByRoomids(roomids));
    }
}
